\section{PPU Details}
Graphics and video output are taken care of by the \textit{Picture Processing Unit}
(PPU) which is running in parallel to the CPU and was also developed by Ricoh. 
The PPU features 256 bytes of on-die \textit{object attribute memory} (OAM) and a main 
working memory, these both are internal to the PPU and separate from the main 
CPU memory and bus. \cite{2C02tech} \\

It has a color palette of 48 colors and 5 grays but only 25 colors can be used at 
the same time on one scanline, where a scanline produces a row of pixels outputted 
to the \textit{cathod ray tube} (CRT) television set. Sprites can be either 8x8 or 8x16 
pixels in size and a total of 64 sprites can be displayed on screen any given time 
without reloading, with a limitation of 8 sprites per scanline. The maximum display 
resolution for the system is 256x240 pixels due to hardware limitation. 
\cite{crtwiki} \\

The PPU also known as \textit{2C02} is running approximately three times faster 
than the CPU depending on the region which equals to 5.37MHz on a NTSC system. 
The static procedure of displaying frames is executed 60 times per second where 
each frame consists of 240 visible scanlines which in turn have 256 pixels each. 
Like the CPU the 2C02 can also address 64KB of memory but it only has 16KB of 
\textit{video ram} (VRAM) used for storing graphics related content such as 
palettes, patterns and name tables. \\

All the communication between the CPU and PPU takes place over the I/O-mapped 
registers and this is the only way the programmer can receive status information 
and control the PPU's behaviour. As previously mentioned these registers reside 
at locations \textit{0x2000-0x2007}. \\

Since the NES was designed to work with the old CRT-based TVs the designers had 
to take the way a TV works into consideration due to the electron gun. It takes 
time to retrace from the bottom of the screen to the top to begin drawing a new 
frame, and this time is called the \textit{vertical blank} (v-blank) period. 
It is only during this time that the CPU can transfer data to the PPU memory without 
risk of graphics corruption since the addresses used by the PPU for drawing can 
get altered when the CPU is writing to the various shared registers. \\

The basic idea of the PPU is to for each scanline go through the correct name 
table entries indexed by the current VRAM-address \cite{loopyppu}. Then continuing with 
the object data from the OAM to find the first eight sprites located on this 
scanline to be drawn on top of the already generated background.
This process is repeated 240 times before the PPU goes into v-blank state. \\

A typical frame takes 263 PPU scanlines, which equals 341*263 cycles, the 
different phases when rendering scanlines are as follows: \cite{neswiki}
\begin{itemize}
    \item 
        0-19: Vertical blank period, this is the time it takes to move 
        the electron gun to the top of screen.
    \item 
        20: A dummy scanline used to initialize internal variables and latches, 
        does not produce any pixels.
    \item 21-261: Visible scanlines, renders pixels, a total of 240 cycles.
    \item 262: Does nothing.
    \item 263: Clean-up and if enabled produces NMI.
\end{itemize}
    
\subsection{Memory Map}
The PPU can also address 64KB of memory but the actual size is only 16KB divided 
into four logical sections as seen in figure \ref{ppumap}. Starting at address \textit{0x0000} 
there are two pattern tables also known as tile tables each \textit{0x1000} in size 
containing background and sprite tile data. These tables are used to store two 
least significant bits of the color data and together with the remaining most 
significant bits from the attribute table define a color used to offset with in 
the palette table.

This implies that each tile is built by reading 16 consecutive bytes where the 
bits from the first eight bytes are used as low bits and together with the bits 
from the last eight bytes form two bit color data, as shown in figure \ref{tc}. 
\cite{nesdoc,2C02tech} 

\begin{figure}[h!]
    \includegraphics[width=\textwidth]{images/tiles.png}
    \caption{Tile composition}
    \label{tc}
\end{figure}

Name tables are used to create the landscape by indexing into the pattern tables
where a byte points to a specific tile. They hold 32x30 tiles which equals 
to a full frame of 256x240 pixels. There are two palette tables, one for the 
sprites and the other one used for background tiles. The first color in each is 
a transparent one used for the background.

\begin{figure}[h!]
    \begin{center}
        \includegraphics[height=300px]{images/ppumap.png}
        \caption{PPU memory map}
        \label{ppumap}
    \end{center}
\end{figure}

\subsection{OAM}
Object attribute memory is a separate memory used to store attributes for a 
maximum of 64 sprites which results in a total size of 256 bytes. The first 
sprite is known as Sprite 0 and is commonly used as a technique for scrolling 
because the PPU will set the hit detection bit in the status register when this 
sprite is drawn on a non-transparent background. By placing sprite 0 in the status 
area together with some clever code one avoids this part of the screen being 
scrolled thus constantly displaying the status information the player needs such 
as remaining health and so on. \cite{nestech}

Each sprite object entry in the OAM uses 4 bytes as follows:
\begin{enumerate}
    \item Y-coordinate
    \item Tile index in the pattern table
    \item Attributes
        \begin{itemize}
            \item Two first bits are the msb of color offset
            \item Bit 5 is priority with respect to the background
            \item Bit 6 is used for horizontal flipping
            \item Bit 7 is used for vertical flipping
        \end{itemize}
    \item X-coordinate
\end{enumerate}

\subsection{PPU Registers}
The I/O-mapped registers used for communication between the CPU 
and the PPU are as follows: \cite{2C02tech}
\begin{itemize}
    \item 0x2000 - Control register 1, write only.
        \begin{enumerate}[start=0]
            \item 
                This combined with the next bit determines what name table
                should be used.
            \item Base table selection, together with bit 0
            \item Increase VRAM address by 1 or 32 when CPU reads 0x2007
            \item Pattern table selection for background tiles.
            \item Pattern table selection for sprites.
            \item Size of sprites, 8x8 or 8x16
            \item Not used.
            \item Enables NMI interrupts at end of frame.
        \end{enumerate}
    \item 0x2001 - Control register 2, write only.
        \begin{enumerate}[start=0]
            \item Turns graphics black and white.
            \item Enable background clipping.
            \item Enable sprite clipping.
            \item Enable background rendering.
            \item Enable sprite rendering.
            \item Intensify red.
            \item Intensify green.
            \item Intensify blue.
        \end{enumerate}
    \item 
        0x2002 - Status, read only. \\
        Note that when when reading, bit 7 is cleared.
        \begin{enumerate}[start=5]
            \item Sprite0 hit detected.
            \item More than 8 sprites in a scanline.
            \item Signals that a vblank has occured.
        \end{enumerate}
    \item 0x2003 - Sprite Address, write only
    \item 0x2004 - Sprite Data, read and write access.
    \item 0x2005 - PPU Scroll, affects VRAM Address, requires 2x writes since the addresses are 16-bits
    \item 0x2006 - PPU Address, affects VRAM Address, requires 2x writes since the addresses are 16-bits
    \item 0x2007 - PPU Data, read and write data to or from the VRAM on the VADDR. 
\end{itemize}

\subsection{Screen Mirroring}
The NES has support for four name tables but due to the memory limitation only 
two can be used at once without using mappers to extend hardware functionality. 
For games without a need for scrolling effects only one name table is used. 
Otherwise horizontal or vertical mirroring is used to produce the scrolling effect. 
To achieve a horizontal scroll one needs to mirror vertically so that the first 
and second name tables are aligned in order and remaining two are mirrors of these. 
Vertical scroll is analogous to this. \cite{nestech}

\subsection{Scrolling}
There are two different ways to scroll the background, horizontal and vertical.
Producing the scrolling effect is done by the programmer by writing to the PPU 
registers \textit{0x2005} and \textit{0x2006}. Since the name tables are 
mirrored only two can be used at a time at most. Each table contains one frame 
worth of pixels and they can either be used separately or in a combination thus 
achieving a scrolling effect by filling in tile data in advance. \cite{nestech}
